package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.FavoriteQuote;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteQuoteMapper {

    @Select("SELECT * FROM favorite_quotes WHERE user_id=#{userId} ORDER BY created_at DESC")
    List<FavoriteQuote> findByUser(Long userId);

    @Insert("INSERT INTO favorite_quotes (user_id, content, source, mood_tag, cover_path) " +
            "VALUES (#{userId}, #{content}, #{source}, #{moodTag}, #{coverPath})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(FavoriteQuote quote);

    @Update("UPDATE favorite_quotes SET content=#{content}, source=#{source}, mood_tag=#{moodTag}, cover_path=#{coverPath} " +
            "WHERE id=#{id} AND user_id=#{userId}")
    int update(FavoriteQuote quote);

    @Delete("DELETE FROM favorite_quotes WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM favorite_quotes WHERE user_id=#{userId} ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<FavoriteQuote> findPage(@Param("userId") Long userId,
                                 @Param("offset") int offset,
                                 @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM favorite_quotes WHERE user_id=#{userId}")
    long countByUser(Long userId);
}
